import BaseItemView from "../../cfw/mvc/BaseItemView";
import { ItemState } from "../../cfw/tools/Define";
import ModuleManager from "../../cfw/module/ModuleManager";
import { ResType } from "../../cfw/res/ResInterface";
import ResItem from "../../cfw/res/ResItem";
import BaseItemModel from "../../cfw/mvc/BaseItemModel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DragItemView extends BaseItemView {

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(cc.Sprite)
    ad: cc.Sprite = null;

    @property(cc.Sprite)
    lock: cc.Sprite = null;

    protected model: any;

    start() {
        super.start();

    }

    private iconName: string = ''
    getIcon() {
        return this.iconName;
    }


    setIcon(fileName: string) {
        this.iconName = fileName
        ModuleManager.getLoader().loadRes(fileName, ResType.SpriteFrame, (err, res: ResItem) => {
            if (err || !cc.isValid(this.icon.node)) {
                return;
            }
            //cc.log(' this.model.getIcon() ', this.model.getIcon())
            this.icon.spriteFrame = res.getRes()
            //cc.log(' this.model.getScale() ', this.model.getScale())
            // this.icon.node.scale = this.model.getScale()
        })
    }

    getID() {
        if (this.model) {
            return this.model.getID()
        }
        return -1;
    }

    init() {
        this.lock.node.active = false
        this.ad.node.active = false;
    }

    // onDestroy(){
    //     super.onDestroy()
    //     if(this.model){
    //         this.model.off(BaseItemModel.UPDATE_STATE, this.updateState, this)
    //     }
    // }
    addListener() {
        this.eventProxy.on(BaseItemModel.UPDATE_STATE, this.updateState, this)
    }
    refresh() {
        // cc.log(' this.model ', this.model)
        // if(this.model){
        //     this.model.off(BaseItemModel.UPDATE_STATE, this.updateState, this)
        // }
        // this.eventProxy.on(BaseItemModel.UPDATE_STATE, this.updateState, this)
        this.setIcon(this.model.getIcon())
        this.updateState()

    }

    updatePosition(pos: cc.Vec2) {
        this.icon.node.x += pos.x;
        this.icon.node.y += pos.y;

        this.ad.node.x += pos.x;
        this.ad.node.y += pos.y;

        this.lock.node.x += pos.x;
        this.lock.node.y += pos.y;
    }

    updateAngle(angle: number) {
        this.icon.node.angle = angle;
        this.ad.node.angle = angle;
        this.lock.node.angle = angle;
    }

    updateScale(x: number, y: number) {
        this.icon.node.scaleX = x;
        this.icon.node.scaleY = y;

        this.ad.node.scaleX = x;
        this.ad.node.scaleY = y;

        this.lock.node.scaleX = x;
        this.lock.node.scaleY = y;
    }


    setVisible(f: boolean) {
        this.icon.node.active = f;
        this.ad.node.active = f;
        this.lock.node.active = f;
    }

    // get node(){
    //     return this.icon.node;
    // }

    isOpen() {
        return this.model.isOpen()
    }

    getOpenLevel() {
        return this.model.getOpenLevel()
    }

    needBuy() {
        return this.model.needBuy()
    }

    updateState() {
        if (!cc.isValid(this.node) || !this.model) {
            return;
        }
        // this.model.setState(ItemState.GOT)
        if (!this.model.needBuy()) {
            if (this.ad) {
                // cc.log(' model id ', this.model.getID())
                this.ad.node.active = false;
            }
        }

        //cc.log('this.model.isOpen() ', this.model.isOpen())
        if (this.model.isOpen()) {
            this.lock.node.active = false
        } else {
            this.lock.node.active = true;
        }
    }

    setState(s: ItemState) {
        this.model.setState(s)
        if (!this.model.needBuy()) {
            if (this.ad) {
                // cc.log(' model ', this.model.getID())
                this.ad.node.active = false;
            }
        } else {
            if (this.ad) {
                // cc.log(' model ', this.model.getID())
                this.ad.node.active = true;
            }
        }
    }


    getBoundingBoxToWorld() {
        return this.icon.node.getBoundingBoxToWorld()
    }

    set x(x: number) {
        this.icon.node.x = x;
    }

    set y(y: number) {
        this.icon.node.y = y;
    }

    get x() {
        return this.icon.node.x;
    }

    get y() {
        return this.icon.node.y;
    }


    getPosition() {
        return this.icon.node.getPosition();
    }
}
